//
//  HomeMVM.swift
//  DianXia
//
//  Created by you&me on 2017/12/15.
//  Copyright © 2017年 you&me. All rights reserved.
//

import Foundation

class HomeVM {

    static let shareHomeVN = HomeVM() //单利  统一操作 首界面和审查列表的数据
    
    let provider = MoyaProvider<BCModuleApi>()
    let dispose = DisposeBag()
    let modelList = Variable([ListModel]())

    func getNeedAuthenList() {

        let mobile = UserManager.shareManager.userMobile ?? ""
        provider.rx.request(.needAuthen(mobile: mobile))
                .retry(3)
                .asObservable()
                .mapModel(NeedAuthenModel.self)
                .subscribe(onNext: { (needModel) in
                    guard needModel.msg == "success" else {
                        return BCShowMessage.shareMessage.showTipMsg(needModel.result?.describe ?? "", time: 1)
                    }
                    self.modelList.value = needModel.result?.list ?? [ListModel()]

                 })
                .disposed(by: dispose)
    }

    func authenResult(_ mobile: String, _ result: String, _ removeIndex: Int) {
        provider.rx.request(.authenResult(mobile: mobile, result: result))
                .retry(3)
                .asObservable()
                .mapModel(NeedAuthenModel.self)
                .subscribe(onNext: { (needModel) in
                 guard needModel.msg == "success" else {
                    return BCShowMessage.shareMessage.showTipMsg(needModel.result?.describe ?? "", time: 1)
                 }
                 self.modelList.value.remove(at: removeIndex)
                 })
                .disposed(by: dispose)
    }
}

struct NeedAuthenModel: HandyJSON {
    var msg: String?
    var result: NeedAuthenListModel?
}

struct NeedAuthenListModel: HandyJSON {
    var describe: String? // 失败的时候的返回
    var list: [ListModel]?
}

struct ListModel: HandyJSON {
    var name: String?
    var mobile: String?
}
